package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 在JDBC中设置事务的隔离级别
 */
public class Demo12 {
    public static void main(String[] args) {
        try ( Connection connection = DBUtil.getConnection();){  //自动关闭特性
            //设置事务哦隔离级别
            //隔离级别在Connection中对应的常量可以进行保留
           connection.setTransactionIsolation(connection.TRANSACTION_READ_COMMITTED);
           connection.setAutoCommit(false);  //将自动提交关闭
            String sql = "INSERT INTO user(username,password,nickname,age )" +
                            "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,"伟超");
            ps.setString(2,"123456");
            ps.setString(3,"超哥");
            ps.setInt(4,22);
            int num  = ps.executeUpdate();
            System.out.println(num>0?"插入成功":"插入失败");
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
